package com.ssm.util;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ssm.util.object.ObjectConstant;


public class SessionFilter implements Filter {
	
	private static final String LOGIN_PAGE = "/wechatPre/html/login/loginJudge.jsp";//登陆界面
	
	private static final String WECHAT_POST = "/wechatPre/wechat/WechatPost";//微信请求--直接通过请求
	
	private static final String ADM_HYT = "/post";
	
	private static final String ADM_GATEWAY = "/gateway";
	
	private static final String HYT_HYT = "/html";
	
	private static final String HYT_ASSETS = "/assets";
	
	private static final String HYT_JS = "/js";
	
	private static final String HYT_HYT_MENU = ".json";
	
	private static final String WECHAT_IMG = "/upload";
	
	public void init(FilterConfig arg0) throws ServletException {
		
	}

	@SuppressWarnings("unchecked")
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// 获得在下面代码中要用的request,response,session对象
		HttpServletRequest servletRequest = (HttpServletRequest) request;
		HttpServletResponse servletResponse = (HttpServletResponse) response;
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		servletResponse.setHeader("Access-Control-Allow-Origin", "*");
		HttpSession session = servletRequest.getSession();
		String url = servletRequest.getContextPath() + servletRequest.getServletPath();
		
		Map<String,Object> user = (Map<String,Object>) session.getAttribute("user");
		if (url.indexOf(HYT_HYT)!=-1) {//html通过
			chain.doFilter(request, response);
		} else if (url.indexOf(HYT_ASSETS)!=-1) {//assets请求通过
			chain.doFilter(request, response);
		} else if (url.indexOf(HYT_JS)!=-1) {//js请求通过
			chain.doFilter(request, response);
		} else if (url.indexOf(ADM_HYT)!=-1) {//post请求通过
			chain.doFilter(request, response);
		} else if (url.indexOf(ADM_GATEWAY)!=-1) {//gateway请求通过
			chain.doFilter(request, response);
		} else if (url.indexOf(WECHAT_POST)!=-1) {//微信请求
			chain.doFilter(request, response);
		} else if (url.indexOf(HYT_HYT_MENU)!=-1) {//menu请求通过
			chain.doFilter(request, response);
		} else if (url.indexOf(WECHAT_IMG)!=-1){//upload请求通过
			chain.doFilter(request, response);
		} else {
			if (url.indexOf(ADM_HYT)==-1) {//post请求通过
				for(Map<String,Object> bean : TokenThread.reqmapping){
					if(url.equals(bean.get("id"))){
						url = bean.get("path").toString().replace(ObjectConstant.PROJECT_NAME, "");
						break;
					}
				}
				if(url.equals(ObjectConstant.PROJECT_NAME_Z)){
					servletResponse.sendRedirect(LOGIN_PAGE);
				}else{
					request.getRequestDispatcher(url).forward(request, response);
				}
			}else{
				if (user == null) {
					if (url.contains(".css") || url.contains(".js")
							|| url.contains(".png") || url.contains(".jpg")
							|| url.contains(".ico") || url.contains(".gif")
							|| url.contains(".eot") || url.contains(".svg")
							|| url.contains(".ttf") || url.contains(".woff")
							|| url.contains(".woff2") || url.contains(".exe")
							|| url.contains(".mp4") || url.contains(".rmvb")
							|| url.contains(".avi") || url.contains(".3gp") 
							|| url.contains(".txt")){
						chain.doFilter(request, response);
					} else {
						servletResponse.sendRedirect(LOGIN_PAGE);
					}
				} else {// 如果用户不为空，放行
					try {
						chain.doFilter(request, response);
					} catch (Exception e) {
						session.removeAttribute("user");
						servletResponse.sendRedirect(LOGIN_PAGE);
					}
				}
			}
		}
	}

	public void destroy() {
		System.out.println("销毁");
	}

}
